package com.elsa.smart_buffet.service.impl;

import com.elsa.smart_buffet.mapper.FeedbackMapper;
import com.elsa.smart_buffet.mapper.ReportMapper;
import com.elsa.smart_buffet.pojo.Feedback;
import com.elsa.smart_buffet.pojo.Report;
import com.elsa.smart_buffet.pojo.ResultBox.CallBackMessage;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
@Transactional
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public ResponseResult selectAllFeedback() {
        return new ResponseResult(CallBackMessage.SUCCESS, reportMapper.selectList(null));
    }

    @Override
    public ResponseResult selectOneByIdFeedback(int id) {
        return new ResponseResult(CallBackMessage.SUCCESS, reportMapper.selectById(id));
    }

    @Override
    public ResponseResult updateFeedback(Report report) {
        return new ResponseResult(CallBackMessage.SUCCESS, reportMapper.updateById(report));
    }

    @Override
    public ResponseResult deleteByIdFeedback(int id) {
        return new ResponseResult(CallBackMessage.SUCCESS, feedbackMapper.deleteById(id));
    }

    @Override
    public ResponseResult insertFeedback(Report report) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        System.out.println(formatter.format(date));
//        report.setFmTime(date.toString());
        return new ResponseResult(CallBackMessage.SUCCESS, reportMapper.insert(report));
    }

    @Override
    public ResponseResult selectFeedbackInOrderByTime(String time) {
        String[] SplitTime = time.split("\\-");
        for (int i = 0; i < SplitTime.length; i++) {
            SplitTime[i] = SplitTime[i].replace("/", "-");
            log.info(SplitTime[i]);
        }
        return new ResponseResult(CallBackMessage.SUCCESS, feedbackMapper.selectReportInOrderByTime(SplitTime[0], SplitTime[1]));
    }

    @Override
    public ResponseResult selectFeedbackBySearchData(String data) {
        return new ResponseResult(CallBackMessage.SUCCESS, feedbackMapper.selectReportBySearchData(data));
    }

    @Override
    public ResponseResult selectReportByCIdAndStatus(String data) {
        return new ResponseResult(CallBackMessage.SUCCESS, feedbackMapper.selectReportByCIdAndStatus(data));
    }

}
